//
//  CourseTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/9/18.
//

import UIKit

class CourseTableViewCell: UITableViewCell {
    var titleBtn: UIButton!
    var collectionView: UICollectionView!
    var articletArray: [ArticleListModel] = []//资讯栏目内容
    var model: CurrentAffairDataModel!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.clipsToBounds = true
        self.selectionStyle = .none
        self.initUI()
    }
    
    func initUI(){
        
        self.titleBtn = UIButton(frame: .zero)
        self.titleBtn.setTitle(MMLocalizedString("car_owner_tutorial", comment: "车主教程"), for: .normal)
        self.titleBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 15)
        self.titleBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.titleBtn.setImage(UIImage(named: "icon_news_arrows"), for: .normal)
        self.titleBtn.semanticContentAttribute = .forceRightToLeft
        self.titleBtn.contentHorizontalAlignment = .left
        self.titleBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 0)
        self.titleBtn.addTarget(self, action: #selector(titleBtnAntion), for: .touchUpInside)
        self.contentView.addSubview(self.titleBtn)
        
       
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: 140, height: 126)
     
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: self.titleBtn.frame.maxY, width: kScreenWidth, height: 130), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
        self.collectionView.backgroundColor = .white
        self.collectionView.showsHorizontalScrollIndicator = false
        self.collectionView.register(CurrentAffairCell.self, forCellWithReuseIdentifier: "CurrentAffairCell")
        self.contentView.addSubview(self.collectionView)
        
        
        self.titleBtn.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(40)
        }
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(self.titleBtn.snp.bottom)
            make.left.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.height.equalTo(130)
            make.bottom.equalTo(0)
        }
        
    }
    
    //setModel
    func setColumnModel(model: CurrentAffairDataModel){
        self.model = model
        self.titleBtn.setTitle(model.name ?? "", for: .normal)
        self.articletArray = model.articleList ?? []
        self.collectionView.reloadData()
    }
    
    
    
    
    //MARK: - Antion
    @objc func titleBtnAntion(){
        let vc = CurrentAffairsViewController()
        vc.model = self.model
        self.viewController().navigationController?.pushViewController(vc, animated: true)
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension CourseTableViewCell: UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.articletArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CurrentAffairCell", for: indexPath) as! CurrentAffairCell
        if self.articletArray.count > indexPath.row{
            let model = self.articletArray[indexPath.row]
            cell.setModel(model: model)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.articletArray.count > indexPath.row{
            let model = self.articletArray[indexPath.row]
            let vc = ArticleDetailsViewController()
            vc.id = "\(model.id ?? 0)"
            vc.titleStr = model.columnName ?? ""
            self.viewController().navigationController?.pushViewController(vc, animated: true)
        }
     
    }
    
    
    
}
